<?php
namespace App\Repository;

use App\Model\MallOrder;

use App\Facade\Event;
use App\Event\MallOrder\StatusNoEvent;

class MallOrderRepository extends \HyperfStore\Repository
{
    // 放入模型
    public function build()
    {
        return new MallOrder;
    }

    // 创建
    public function create(UserRepository $user, MallGoodsRepository $goods, $data)
    {
        $this->model->user()->associate($user->model());
        $this->model->goods()->associate($goods->model());
        
        $data['shop_id'] = $goods->shop_id;
        $data['goods_name'] = $goods->name;
        $data['goods_image'] = $goods->images[0] ?? '';
        
        return $this->exception(function() use($data) {
            return $this->save($data);
        });
    }
    
    // 需要支付的金额
    public function payMoney()
    {
        return bcadd($this->model->money_pay, $this->model->money_express, 2);
    }

    // 判断订单已支付
    public function statusPay()
    {
        return $this->model->status > MallOrder::STATUS_POST;
    }
    
    // 判断是否可退款，发货之前都可退
    public function statusRefund()
    {
        return $this->model->status < MallOrder::STATUS_EXPRESS && $this->model->status > MallOrder::STATUS_NO;
    }
    
    // 判断订单已发货
    public function statusExpress()
    {
        return $this->model->status == MallOrder::STATUS_EXPRESS;
    }
    
    // 判断是否完成订单
    public function statusOk()
    {
        return $this->model->status == MallOrder::STATUS_OK;
    }
    
    // 判断是否关闭
    public function statusNo()
    {
        return $this->model->status == MallOrder::STATUS_NO;
    }
    
    // 设置已支付
    public function setStatusPay($pay = '')
    {
        if(!$this->statusOk())
        {
            $this->model->status = MallOrder::STATUS_PAY;
            $this->model->pay = $pay;
            $this->model->pay_time = time();
        
            return $this->model->save();
        }
    }
    
    // 发货操作
    public function setExpress($name, $code)
    {
        if($this->statusPay())
        {
            $this->model->express = $name ?? '';
            $this->model->express_code = $code ?? '';
            $this->model->status = MallOrder::STATUS_EXPRESS;
            $this->model->express_time = time();
            
            return $this->model->save();
        }
    }
    
    // 确认收货
    public function setStatusOk()
    {
        if($this->statusExpress())
        {
            $this->model->status = MallOrder::STATUS_OK;
            $this->model->ok_time = time();
                
            return $this->model->save();
        }
    }
    
    // 关闭订单
    public function setStatusNo()
    {
        if($this->statusRefund())
        {
            Event::dispatch(new StatusNoEvent($this));
            
            $this->model->status = MallOrder::STATUS_NO;
        
            return $this->model->save();
        }
    }
    
    // 更新为已结算
    public function settleOk($id)
    {
        return $this->model->whereIn('id', (array) $id)->update([
            'status_settle' => MallOrder::STATUS_SETTLE_OK
        ]);
    }
}